/**
 @file Announce_Handler.h

 Handles replying messages reacted by multicast messages in MDNS

 @author Kyoungho An <kyoungho.an@gmail.com>
*/

#ifndef _ANNOUNCE_HANDLER_H_
#define _ANNOUNCE_HANDLER_H_

#include "ace/Reactor.h"
#include "ace/Log_Msg.h"
#include "ace/Event_Handler.h"
#include "ace/Time_Value.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Dgram_Mcast.h"

// include MDNS related library
extern "C" {
#include "mdns.h"
#include "mdnsd.h"
}

// define MDNS address and port
#define MDNS_ADDR "224.0.0.251"
#define MDNS_PORT 5353

// define socket buffer size
#define BUFFER_SIZE 65536

namespace MDNS {
class Announce_Handler : public ACE_Event_Handler {
public:

	// constructor
	Announce_Handler (ACE_Reactor *r = ACE_Reactor::instance ());

	// destructor
	~Announce_Handler (void) { }

	// initialize multicast socket and register the reactor handler
	int open (struct mdnsd *svr);

	// periodically check and send announce record
	virtual int handle_timeout (const ACE_Time_Value &current_time, 
	const void* =0 );

	// close the handler with deleting allocated memory
	virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE);
private:
	// multicast socket
	ACE_SOCK_Dgram_Mcast mcast_dgram_;
	// multicast address
	ACE_INET_Addr mcast_addr_;
	// MDNS daemon object
	struct mdnsd *svr_;
	// replying packet object
	struct mdns_pkt *mdns_reply_;
	// socket buffer
	ACE_UINT8 pkt_buf_[BUFFER_SIZE];
};
}

#endif
